/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bio.jsf;

import com.bio.jpa.entities.Project;
import com.bio.jpa.entities.User;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Diego
 */
@ManagedBean(name = "serverController")
@ApplicationScoped
public class ServerController {

    private PieChartModel pieModel = new PieChartModel();
    private CartesianChartModel lineChartModel = new CartesianChartModel();
    private HashMap<String, String> info = new HashMap<String, String>();
    private HashMap<String, Integer> jvm_info = new HashMap<String, Integer>();
    int mb = 1024 * 1024;

    public HashMap<String, Integer> getJvm_info() {
        return jvm_info;
    }

    public void setJvm_info(HashMap<String, Integer> jvm_info) {

        this.jvm_info = jvm_info;
    }

    public PieChartModel getPieModel() {
        Runtime runtime = Runtime.getRuntime();
        //Print used memory
//        System.out.println("Used Memory:"
//                + (runtime.totalMemory() - runtime.freeMemory()) / mb);
        pieModel.set("Used Memory:", (runtime.totalMemory() - runtime.freeMemory()) / mb);
        //Print free memory
//        System.out.println("Free Memory:"
//                + runtime.freeMemory() / mb);
        pieModel.set("Free Memory:", runtime.freeMemory() / mb);
        //Print total available memory
//        System.out.println("Total Memory:" + runtime.totalMemory() / mb);
        pieModel.set("Total Memory:", (runtime.totalMemory() / mb));
        //Print Maximum available memory
//        System.out.println("Max Memory:" + runtime.maxMemory() / mb);;
        pieModel.set("Max Memory:", runtime.maxMemory() / mb);
        return pieModel;
    }

    public CartesianChartModel getLineModel() {
        ChartSeries users = new ChartSeries("Users");
        ChartSeries projects = new ChartSeries("Projects");

        ProjectController controller = (ProjectController) FacesContext.getCurrentInstance().getApplication().getELResolver().
                getValue(FacesContext.getCurrentInstance().getELContext(), null, "projectController");
        UserController controller2 = (UserController) FacesContext.getCurrentInstance().getApplication().getELResolver().
                getValue(FacesContext.getCurrentInstance().getELContext(), null, "userController");

        List<Project> projectList = controller.getItems();
        List<User> userList = controller2.getUsers();
        SimpleDateFormat sdf;

        sdf = new SimpleDateFormat("MMyyyy");
        Map<Object, Number> stat = new HashMap<Object, Number>();
        for (User user : userList) {
            if (stat.containsKey(sdf.format(user.getUserCreation()))) {
                Integer nb = (Integer) stat.get(sdf.format(user.getUserCreation()));
                stat.put(sdf.format(user.getUserCreation()), nb.intValue() + 1);
            } else {
                stat.put(sdf.format(user.getUserCreation()), 1);
            }
        }
        users.setData(stat);
        //  stat.clear();
        Map<Object, Number> stat2 = new HashMap<Object, Number>();
        sdf = new SimpleDateFormat("MMyyyy");
        for (Project p : projectList) {
            if (stat2.containsKey(sdf.format(p.getPrjCreation()))) {
                Integer tmp = (Integer) stat2.get(sdf.format(p.getPrjCreation()));
                stat2.put(sdf.format(p.getPrjCreation()), (tmp + 1));
            } else {
                stat2.put(sdf.format(p.getPrjCreation()), 1);
            }
        }
        System.out.println(stat2);
        projects.setData(stat2);
        // stat.clear();

        lineChartModel.clear();
        lineChartModel.addSeries(users);
        lineChartModel.addSeries(projects);
        return lineChartModel;
    }

    public void setLineChartModel(CartesianChartModel lineChartModel) {
        this.lineChartModel = lineChartModel;
    }

    public void setPieModel(PieChartModel pieModel) {
        this.pieModel = pieModel;
    }

    public ServerController() {
        Set<String> names = System.getProperties().stringPropertyNames();
        for (String string : names) {
            if (string.startsWith("user") || string.startsWith("os")) {
                info.put(string, System.getProperties().get(string).toString());
            }
        }


    }

    public HashMap<String, String> getInfo() {
        return info;
    }

    public void setInfo(HashMap<String, String> info) {
        this.info = info;
    }

    public static void main(String args[]) {

        new ServerController();

    }
}
